<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="排产状态" prop="wfeedStatus">
        <el-select v-model="queryParams.wfeedStatus" placeholder="请选择" clearable>
          <el-option
            v-for="dict in orderStatusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择" clearable>
          <el-option v-for="team in teamList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号" prop="contractCode">
        <el-input
          v-model="queryParams.contractCode"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="teamSelect"
          @click="handlePlan"
          v-hasPermi="['factory:factorywfeed:edit']"
        >执行投料</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              border style="width:100%;" :data="factoryWorkOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  :selectable="handleDisable"/>
      <el-table-column label="班组名称" align="center" prop="teamName" />
      <el-table-column label="订单合同号" align="center" prop="contractCode" />
      <el-table-column label="产品编码" align="center" prop="bomCode" />
      <el-table-column label="产品名称" align="center" prop="bomName" />
      <!--<el-table-column label="规格型号" align="center" prop="productStandard" />
      <el-table-column label="产品单位" align="center" prop="productUnit" />-->
      <el-table-column label="产品数量" align="center" prop="workerOrdernum" />
      <el-table-column label="待投料数量" align="center" prop="planNum" />
      <el-table-column label="已投料数量" align="center" prop="wfeedNum" />
      <!--<el-table-column label="排产说明" align="center" prop="planDesc" />
      <el-table-column label="分厂名称" align="center" prop="factoryName" />
      <el-table-column label="排产开始日期" align="center" prop="planStartdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planStartdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排产完成时间" align="center" prop="planFinishdate" />-->
       <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.storeStatus == '0'"
            @click="handlePlan(scope.row)"
            v-hasPermi="['factory:factorywfeed:edit']"
          >执行投料</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog title="周投料计划" :visible.sync="open" width="1400px" :close-on-click-modal ="false" append-to-body>
      <div style="float: right;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px"  style="margin-bottom: 10px">
         <!-- <el-date-picker clearable
            v-model="form.planStartdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始日期">
          </el-date-picker>
          -
          <el-date-picker clearable
            v-model="form.planEnddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始日期">
          </el-date-picker> -->
          <el-button type="primary" @click="excutePlan" style="margin-left: 20px;">投料计算</el-button>
          <!--<el-button type="primary" @click="excutePlan" style="margin-left: 20px;">打印投料计划</el-button>-->
        </el-form>
      </div>
      <el-table v-loading="loading" :data="planWorkOrderList" >
        <el-table-column label="订单合同号" align="center" prop="contractCode" />
        <el-table-column label="产品编号" align="center" prop="bomCode" />
        <el-table-column label="产品名称" align="center" prop="bomName" />
        <!--<el-table-column label="产品型号" align="center" prop="orderCode" />-->
        <el-table-column label="分单数量" align="center" prop="workerOrdernum" />
        <el-table-column label="待投料数量" align="center"  prop="planNum">
          <template slot-scope="scope">
            <span> {{ scope.row.planNum-scope.row.wfeedNum }} </span>
          </template>
        </el-table-column>
        <el-table-column label="班组名称" align="center" prop="teamName" />
         <!--<el-table-column label="产能情况" align="center" prop="capacityDesc" />
        <el-table-column label="库存情况" align="center" prop="inventoryDesc" />-->
        <el-table-column label="本周投料数量" align="center" prop="planFeednum"/>
        <el-table-column label="投料开始时间" align="center" prop="wplanStartDate" />
        <el-table-column label="投料完成时间" align="center" prop="wplanEndDate" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.wplanEndDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handPlanRemoveOrder(scope.row)"
              v-hasPermi="['factory:factorywfeed:edit']"
            >移除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />-->
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary" @click="submitPlan" >提交排产</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWfeedWorkorder,listChooseWfeedWorkorder } from "@/api/sm/order/workorder";
import { excuteFactoryWFeed,submitFactoryWFeed } from "@/api/sm/factory/factorywfeed";
import { listTeam } from "@/api/system/dept";

export default {
  name: "Orderinfo",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      teamSelect: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      plantotal: 0,
      //分厂待排产订单信息表格数据
      factoryWorkOrderList: [],
      //排产工单
      planWorkOrderList: [],
      //排产结果
      planResultList: [],
      teamList: [],
      orderStatusList: [
        {"label":"待排产",
         "value":0
        },
        {"label":"排产完成",
         "value":1
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 同步时间时间范围
      daterangeOrderDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamId: null,
        bomCode: null,
        wfeedStatus: 0,
        workerConfirmstatus: 1,
        planStatus: 1,
        mplanStatus: 1,
        workerOrdertype: 1
      },
      // 表单参数
      form: {
        planMonth: null,
        workOrderIds: null
      },
      // 表单参数
      form1: {
        wfeedList: null
      },
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
    this.getTeamList();
  },
  methods: {
     /** 查询订单信息列表 */
     getTeamList() {
      this.loading = true;
      listTeam().then(response => {
        this.teamList = response.data;
      });
    },
    handleDisable(row, index) {
      if (row.storeStatus == '1') {
        return false
      } else {
        return true
      }
    },
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listWfeedWorkorder(this.queryParams).then(response => {
        this.factoryWorkOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planChooseMonth: null,
        workOrderIds: null
      };
      this.resetForm("form");
    },
    reset1() {
      this.form1 = {
        wfeedList: null
      };
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.queryParams.teamId!=null && this.queryParams.teamId!=undefined){
        this.teamSelect = false;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
     /** 执行排产操作 */
    handlePlan(row) {
      this.reset1();
      this.loading = true;
      const ids = row.id || this.ids;
      this.orderplanList = null;
      listChooseWfeedWorkorder({"ids":ids+''}).then(response => {
        this.planWorkOrderList = response.data;
        this.open = true;
        //this.title = "待排产工单";
        this.loading = false;
      });
    },
    handPlanRemoveOrder(row) {
      const id = row.id
      this.planWorkOrderList.map((item,index)=>{
      　　if(item.id==id)
          this.planWorkOrderList.splice(index,1);//index 当前元素索引；1：需要删除的元素个数
      });
    },

    excutePlan() {
      if(this.planWorkOrderList.length==0){
        alert("无投料计划工单");
        return;
      }

      this.form.workOrderIds=this.planWorkOrderList.map(item => item.id).join(',')
      excuteFactoryWFeed(this.form).then(response => {
        this.planWorkOrderList = response.data;
        //this.orderinfoResultList = response.data.orderInfoList;
        //this.setOrderPlanResult();
        this.$modal.msgSuccess("投料计划成功");
        //this.open = false;
        //this.getList();
      });
    },

  submitPlan() {
    if(this.planWorkOrderList.length==0){
      alert("无投料计划详情");
      return;
    }

    this.form1.wfeedList=this.planWorkOrderList;
    submitFactoryWFeed(this.form1).then(response => {
      this.$modal.msgSuccess("提交成功");
      this.open = false;
      this.getList();
    });
  },
  }
};
</script>
